﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DeskNotes.Utils
{
    /// <summary>
    /// Utility class for handling RTF formatting done via RichText EM_ messages.
    /// </summary>
    static class RtfFormats
    {

        /// <summary>
        /// Returns list type for selected paragraph from txtNote
        /// </summary>
        /// <returns></returns>
        internal static short GetListType(RichTextBox rtb)
        {
            PARAFORMAT2 paraformat1 = new PARAFORMAT2();
            paraformat1.dwMask = (int)(WinImports.PFM_NUMBERING);
            WinImports.SendMessage(new System.Runtime.InteropServices.HandleRef(rtb, rtb.Handle), WinImports.EM_GETPARAFORMAT, 0, paraformat1);
            //Logger.WriteLine("Current para style; wNumbering: " + paraformat1.wNumbering);
            return paraformat1.wNumbering;
        }

        /// <summary>
        /// Finds next list type after currently set list by call SendMessage(... EM_SETPARAFORMAT...)
        /// If EM_GETPARAFORMAT is called immediatelly after this then it returns same list type. Kind of behaviour of RTF
        /// <param name="listType">list type set in EM_SETPARAFORMAT call</param>
        /// <returns>list type following the one returned.</returns>
        internal static short GetListTypeAfterMessage(short listType)
        {
            WinImports.AdvRichTextBulletType current = (WinImports.AdvRichTextBulletType)listType;
            bool nextStop = false;
            foreach (WinImports.AdvRichTextBulletType type in Enum.GetValues(typeof(WinImports.AdvRichTextBulletType)))
            {
                //previous iteration found same list type as set in message then return next after it
                if (nextStop)
                    return (short)type;
                // if list type is same as this one then stop on next iteration
                if (current == type)
                    nextStop = true;
            }
            return (short)WinImports.AdvRichTextBulletType.NoList;
        }

        /// <summary>
        /// Set On or Off numbered list at current paragraph
        /// </summary>
        /// <param name="TurnOn">true for numbering to be set on, otherwise false</param>
        /// <returns>numbering type set during call</returns>
        internal static short NumberedBulletList(RichTextBox rtb, bool TurnOn, short listType)
        {
            PARAFORMAT2 paraformat1 = new PARAFORMAT2();
            paraformat1.dwMask = (int)(WinImports.PFM_NUMBERING | WinImports.PFM_OFFSET | WinImports.PFM_NUMBERINGSTYLE | WinImports.PFM_NUMBERINGSTART | WinImports.PFM_NUMBERINGTAB);
            if (!TurnOn)
            {
                paraformat1.wNumbering = 0;
                paraformat1.dxOffset = 0;
            }
            else
            {
                paraformat1.wNumbering = listType;
                paraformat1.dxOffset = rtb.BulletIndent * MainForm.TwipsPerPixel;
                paraformat1.wNumberingStyle = (short)WinImports.AdvRichTextBulletStyle.Period; // (short)_BulletStyle;
                paraformat1.wNumberingStart = 1; // _BulletNumberStart;
            }

            WinImports.SendMessage(new System.Runtime.InteropServices.HandleRef(rtb, rtb.Handle), WinImports.EM_SETPARAFORMAT, 0, paraformat1);
            return paraformat1.wNumbering; // GetListTypeAfterMessage(paraformat1.wNumbering);
        }

        /// <summary>
        /// Determines if current selection is numbered list.
        /// </summary>
        /// <returns>true if current selection or paragraph is numbered list, otherwise false</returns>
        internal static bool SelectionNumbered(short listType)
        {
            //short listType = GetListType();
            short normalBullet = (short)WinImports.AdvRichTextBulletType.Normal;
            if (listType > normalBullet)
                return true;
            else
                return false;
        }
    }
}
